﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace SuperJoe.Framework.Common
{
    public class MD5
    {
        public const string SALT = "jfkewuqrioemvc.,zxvrioew";

        /// <summary>
        /// 3DES 加密
        /// </summary>
        /// <param name="a_strString">要加密的字符串</param>
        /// <param name="a_strKey">KEY</param>
        /// <returns></returns>
        public static string Encrypt3DES(string a_strString, string a_strKey)
        {
            TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();

            DES.Key = ASCIIEncoding.ASCII.GetBytes(a_strKey);
            DES.Mode = CipherMode.ECB;

            ICryptoTransform DESEncrypt = DES.CreateEncryptor();

            byte[] Buffer = ASCIIEncoding.ASCII.GetBytes(a_strString);
            return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
        }

        /// <summary>
        /// 3DES 加密 使用默认的加密字符串
        /// </summary>
        /// <param name="a_strString">要加密的字符串</param>
        /// <returns></returns>
        public static string Encrypt3DES(string a_strString)
        {
            return MD5.Encrypt3DES(a_strString, SALT);
        }
        /// <summary>
        /// 3DES 解密
        /// </summary>
        /// <param name="a_strString">要解密的字符串</param>
        /// <param name="a_strKey">KEY</param>
        /// <returns></returns>
        public static string Decrypt3DES(string a_strString, string a_strKey)
        {
            TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();

            DES.Key = ASCIIEncoding.ASCII.GetBytes(a_strKey);
            DES.Mode = CipherMode.ECB;
            DES.Padding = System.Security.Cryptography.PaddingMode.PKCS7;

            ICryptoTransform DESDecrypt = DES.CreateDecryptor();

            string result = "";
            try
            {
                byte[] Buffer = Convert.FromBase64String(a_strString);
                result = ASCIIEncoding.ASCII.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
            }
            catch (Exception ee)
            {
                return ee.ToString();
            }
            return result;
        }

        /// <summary>
        /// 3DES 解密 使用默认的加密字符串
        /// </summary>
        /// <param name="a_strString">要加密的字符串</param>
        /// <returns></returns>
        public static string Decrypt3DES(string a_strString)
        {
            return MD5.Decrypt3DES(a_strString, SALT);
        }
    }
}

